package com.clkj.tramcarlibrary.tram;

import android.support.annotation.NonNull;
import android.util.Log;

import com.clkj.tramcarlibrary.entity.StopDetail;
import com.clkj.tramcarlibrary.entity.StopsDistance;
import com.clkj.tramcarlibrary.entity.Tramcar;
import com.clkj.tramcarlibrary.http.HttpService;
import com.clkj.tramcarlibrary.schedulers.BaseSchedulerProvider;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/2/14.
 */

public class TramcarPresenter implements TramcarContract.Presenter {

    @NonNull
    TramcarContract.View mTramcarView;

    @NonNull
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private final HttpService mHttpService;

    @NonNull
    private CompositeSubscription mSubscriptions;

    public TramcarPresenter(@NonNull TramcarContract.View mTramcarView, @NonNull BaseSchedulerProvider mSchedulerProvider, @NonNull HttpService mHttpService) {
        this.mTramcarView = mTramcarView;
        this.mSchedulerProvider = mSchedulerProvider;
        this.mHttpService = mHttpService;
        this.mSubscriptions = new CompositeSubscription();

        this.mTramcarView.setPresenter(this);
    }


    //获取站点信息
    @Override
    public void getTramcarStops() {
        mTramcarView.showLoading();
        Subscription subscription = mHttpService.getTramcarStops()
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {
                        //Log.e("电车", jsonObject.toString());

                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mTramcarView.dismissLoading();
                            mTramcarView.showGetTramcarStopsError("获取所有站点失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mTramcarView.dismissLoading();
                        mTramcarView.showError("服务器获取数据失败");

                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        mTramcarView.dismissLoading();
                        Log.e("电车", jsonObject.toString());
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();
                        List<Tramcar> tramcarList = new Gson().fromJson(jsonArray, new TypeToken<List<Tramcar>>() {
                        }.getType());
                        mTramcarView.showTramcarStops(tramcarList);
                    }
                });
        mSubscriptions.add(subscription);
    }

    //获取距离你最近的站点信息
    @Override
    public void getRecentStop(String lat, String lon) {
        mTramcarView.showLoading();
        Subscription subscription = mHttpService.getRecentStop(lat, lon)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {

                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mTramcarView.dismissLoading();
                            mTramcarView.showRecentStopError("获取最近站点失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.e("失败", "失败");
                        mTramcarView.dismissLoading();
                        mTramcarView.showError("数据获取异常");
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        mTramcarView.dismissLoading();
                        Log.e("站点信息", jsonObject.toString());
                        JsonObject dataObject = jsonObject.get("data").getAsJsonObject();

                        String station_name = dataObject.get("station_name").getAsString();
                        String distance = dataObject.get("distance").getAsString();
                        mTramcarView.showRecentStop(station_name, distance);
                    }
                });
        mSubscriptions.add(subscription);
    }

    //获取所有距离的站点信息
    @Override
    public void getAllDistance(String lat, String lon) {
        mTramcarView.showLoading();
        Subscription subscription = mHttpService.getAllDistance(lat, lon)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {

                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        } else {
                            mTramcarView.dismissLoading();
                            mTramcarView.showAllDistanceError("获取站点距离失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mTramcarView.showError("获取站点距离失败,请重新获取");
                        mTramcarView.dismissLoading();
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        mTramcarView.dismissLoading();
                        Log.e("所有站点距离", jsonObject.toString());
                        //{"status":"success","message":"获取成功","data":[{"station_name":"体育馆站","distance":7155},{"station_name":"淮海东路站","distance":6765},{"station_name":"运河广场站","distance":6413},{"station_name":"老坝口站","distance":6492},{"station_name":"水渡口站","distance":6652},{"station_name":"政务中心站","distance":6195},{"station_name":"深圳路站","distance":5686},{"station_name":"厦门路站","distance":5390},{"station_name":"八亭桥站","distance":5180},{"station_name":"汕头路站","distance":5174},{"station_name":"三亚路站","distance":5381},{"station_name":"板闸站","distance":5858},{"station_name":"大剧院站","distance":6222},{"station_name":"森林公园站","distance":6739},{"station_name":"乌沙河站","distance":7593},{"station_name":"河下站","distance":8500},{"station_name":"礼字坝站","distance":9480},{"station_name":"古末口站","distance":10514},{"station_name":"华西路站","distance":10723},{"station_name":"纪念馆站","distance":10964},{"station_name":"东岳庙站","distance":11289},{"station_name":"龙光阁站","distance":11327},{"station_name":"南门站","distance":11483}]}
                        JsonArray jsonArray = jsonObject.get("data").getAsJsonArray();
                        List<StopsDistance> stopsDistanceList = new Gson().fromJson(jsonArray, new TypeToken<List<StopsDistance>>() {
                        }.getType());
                        mTramcarView.showAllDistance(stopsDistanceList);
                    }
                });
        mSubscriptions.add(subscription);
    }

    //获取站点的详情
    @Override
    public void getStopDetail(String station_name) {
        mTramcarView.showLoading();
        Subscription subscription = mHttpService.getStopDetail(station_name)
                .subscribeOn(mSchedulerProvider.computation())
                .observeOn(mSchedulerProvider.ui())
                .filter(new Func1<JsonObject, Boolean>() {
                    @Override
                    public Boolean call(JsonObject jsonObject) {

                        if (jsonObject.get("status").getAsString().equals("success")) {
                            return true;
                        }
                        else {
                            mTramcarView.dismissLoading();
                            mTramcarView.showStopDetailError("获取站点详情失败,请重新获取");
                        }
                        return false;
                    }
                })
                .subscribe(new Observer<JsonObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mTramcarView.showError("获取站点详情失败,请重新获取");
                        mTramcarView.dismissLoading();
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        mTramcarView.dismissLoading();
                        Log.e("站点的详情", jsonObject.toString());
                        JsonObject object = jsonObject.get("data").getAsJsonObject();

                        StopDetail stopDetail = new Gson().fromJson(object, StopDetail.class);

                        mTramcarView.showStopDetail(stopDetail);
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.unsubscribe();
        mSubscriptions = null;
    }

}
